import numpy as np
import matplotlib.pyplot as plt

# 中文显示
plt.rcParams['font.sans-serif'] = ['Microsoft YaHei','DV Sans']
plt.rcParams['axes.unicode_minus'] = False

def euler_method(f, x0, y0, h, n):
    """
    欧拉法求解微分方程
    f: 微分方程dy/dx = f(x, y)
    x0, y0: 初始条件
    h: 步长
    n: 步数
    """
    x = np.zeros(n+1)
    y = np.zeros(n+1)
    x[0] = x0
    y[0] = y0
    
    for i in range(n):
        y[i+1] = y[i] + h * f(x[i], y[i])
        x[i+1] = x[i] + h
    
    return x, y

# 示例：求解dy/dx = x + y, y(0)=1
def f(x, y):
    return x + y

x, y = euler_method(f, 0, 1, 0.1, 100)
plt.plot(x, y)
plt.xlabel('x')
plt.ylabel('y')
plt.title('欧拉法求解微分方程')
plt.grid(True)
plt.show()